package com.everflourish.yeah100.act.questionlib;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.everflourish.yeah100.R;
import com.everflourish.yeah100.act.base.BaseActivity;
import com.everflourish.yeah100.entity.question.Question;
import com.everflourish.yeah100.entity.question.QuestionUploadImage;
import com.everflourish.yeah100.http.QuestionRequest;
import com.everflourish.yeah100.ui.dialog.LoadDialog;
import com.everflourish.yeah100.utils.IntentUtil;
import com.everflourish.yeah100.utils.LogUtil;
import com.everflourish.yeah100.utils.MyToast;
import com.everflourish.yeah100.utils.StringUtil;
import com.everflourish.yeah100.utils.constant.Constant;
import com.everflourish.yeah100.utils.constant.PaperType;
import com.everflourish.yeah100.utils.constant.ResultCode;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 作者：Created by liqisi on 2016/7/25.
 * 时间：2016/7/25
 */
public class QuestionCommentActivity extends BaseActivity {

    private Toolbar mToolBar;
    public QuestionRequest mRequest;
    private EditText mCommentTv;
    private Question mQuestion;
    private LoadDialog mLoadDialog;
    private String mExaminationId;
    private boolean isUpdate = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question_comment);
        initData();
        initView();
    }

    private void initData() {
        mRequest = QuestionRequest.getInstance();
        mQuestion = (Question) getIntent().getSerializableExtra(IntentUtil.QUESTION_UPDATE);
        mExaminationId = getIntent().getStringExtra(IntentUtil.EXAMINATION_ID);
    }

    private void initView() {
        mCommentTv = (EditText) findViewById(R.id.question_comment_input);
        mCommentTv.setText(mQuestion.getComment());
        mCommentTv.setSelection(mCommentTv.getText().length());
        mToolBar = (Toolbar) findViewById(R.id.toolbar_question_comment);
        mToolBar.setTitle("题目解析");
        setSupportActionBar(mToolBar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishActivity();
            }
        });
        mToolBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.question_comment_summit:
                        questionUpdateRequest(setUploadImagePara());
                        break;
                }
                return true;
            }
        });
    }

    /**
     * 修改题目信息请求
     *
     * @param questionUploadImage
     */
    private void questionUpdateRequest(QuestionUploadImage questionUploadImage) {
        mLoadDialog = LoadDialog.show(mContext, null, "正在修改...", mQueue);
        JsonObjectRequest request = mRequest.questionUpdateRequest(questionUploadImage,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        questionUpdateListener(response);
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        mRequest.disposeError(mContext, mLoadDialog, error, "添加解析失败");
                    }
                });
        mQueue.add(request);
    }

    /**
     * 题目增加图片监听
     *
     * @param response
     */
    private void questionUpdateListener(JSONObject response) {
        String resultCode = "";
        try {
            resultCode = response.getString(Constant.RESULT_CODE);
            if (resultCode.equals(ResultCode.result_ok.resultCode)) {
                String questionId = response.getString("questionId");
                if (StringUtil.stringIsNull(questionId)) {
                    MyToast.showLong(mContext, "添加解析失败");
                    return;
                }
                isUpdate = true;
                finishActivity();
                MyToast.showShort(mContext, "添加解析成功");
            } else if (resultCode.equals(ResultCode.result_000001E.resultCode)) {
                MyToast.showShort(mContext, R.string.questionlib_09161_000001E);
            } else if (resultCode.equals(ResultCode.result_300001E.resultCode)) {
                MyToast.showShort(mContext, R.string.questionlib_09162_300001E);
            } else if (resultCode.equals(ResultCode.result_300024E.resultCode)) {
                MyToast.showShort(mContext, R.string.questionlib_09163_300024E);
            } else if (resultCode.equals(ResultCode.result_300025E.resultCode)) {
                MyToast.showShort(mContext, R.string.questionlib_09164_300025E);
            } else if (resultCode.equals(ResultCode.result_system_exception_999999E)) {
                MyToast.showShort(mContext, R.string.questionlib_091699_999999E);
            } else {
                MyToast.showShort(mContext, "添加解析失败");
            }
        } catch (JSONException e) {
            LogUtil.e("JSON异常", e);
            MyToast.showShort(mContext, "添加解析失败");
        } finally {
            LoadDialog.dismiss(mLoadDialog);
        }
    }

    /**
     * 设置修改题目信息的参数
     *
     * @return
     */
    private QuestionUploadImage setUploadImagePara() {
        QuestionUploadImage questionUploadImage = new QuestionUploadImage();
        questionUploadImage.setExaminationId(mExaminationId);
        questionUploadImage.setQuestionId(mQuestion.getQuestionId());
        questionUploadImage.setPaperType(PaperType.NONE);
//        questionUploadImage.setSeqNo(mQuestion.getSeqNo());
//        questionUploadImage.setImageUrls(mQuestion.getImageUrls());
//        questionUploadImage.setQuestionType(Integer.valueOf(mQuestion.getQuestionType()));
        questionUploadImage.setComment(mCommentTv.getText().toString());
        return questionUploadImage;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finishActivity();
            return true;
        } else {
            return super.onKeyUp(keyCode, event);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mQueue.stop();
    }

    private void finishActivity() {
        Intent resultIntent = getIntent();
        resultIntent.putExtra(IntentUtil.QUESTION_IS_UPDATE, isUpdate);
        this.setResult(RESULT_OK, resultIntent);
        IntentUtil.finishActivity(QuestionCommentActivity.this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_question_comment, menu);
        return true;
    }
}
